//
//  LJCFirstPageControllerVC.m
//  WZLProduct
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#define kSpacing 3
#define KEYHEADER @"3590069"

#import "LJCFirstPageControllerVC.h"
#import "Set.h"


@interface LJCFirstPageControllerVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *rootCollection;/**< 底层Collection */

@property (nonatomic, retain) NSArray *titleArr;/**< 标题数组 */
@property (nonatomic, retain) NSArray *imageArr;/**< 图片数组 */
@property (nonatomic, retain) NSMutableArray *recommendArr;/**< 推荐数组 */
@property (nonatomic, retain) NSMutableArray *topTingArr;/**< 排行数组 */
@property (nonatomic, retain) NSMutableArray *hotRadioArr;/**< 热门数组 */
@property (nonatomic, retain) NSMutableArray *voicedArr;/**< 最新发声数组 */
@property (nonatomic, retain) NSArray *dataArr;/**< 总数组 */

@end

@implementation LJCFirstPageControllerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    [self createCollection];
    _titleArr = @[@"爱情", @"旅行", @"故事", @"音乐", @"电影", @"读诗"];
    _imageArr = @[@"iconfont-xin.png", @"iconfont-shu.png", @"iconfont-zhuanzhixingliyuan.png", @"iconfont-weibiaoti4.png", @"iconfont-dianying.png", @"iconfont-yuedu.png"];
    [self dataHandel];
    
}

/** 数据解析 */
- (void)dataHandel {
    /** 推荐的数据解析 */
    [LJCAFN POSTWithURL:@"http://api.pianke.me/version4.0/ting/listOfRecommend.php?sig=22A5BE98C01AFBE3142E550879F94685" prefix:KEYHEADER par:@{@"pageSize" : @"6"} success:^(id response) {

        _recommendArr = [LJCMusicModel modelHandleWithModel:response[@"data"]];/**< kvc赋值 */

        [_rootCollection reloadData];
    } filed:^(NSError *err) {
        
    }];
    
    /** 排行数据解析 */
    [LJCAFN POSTWithURL:@"http://api.pianke.me/version4.0/ting/list.php?sig=16784DD9E11493590046D7AD7DA73E2C" prefix:KEYHEADER par:@{@"pageSize" : @"3", @"sort" : @"2"} success:^(id response) {
        
        _topTingArr = [LJCMusicModel modelHandleWithModel:response[@"data"]];/**< kvc */
        
        [_rootCollection reloadData];
    } filed:^(NSError *err) {
        
    }];
    
    /** 热门电台 */
    [LJCAFN POSTWithURL:@"http://api.pianke.me/version4.0/ting/listOfRadio.php?sig=22A5BE98C01AFBE3142E550879F94685" prefix:KEYHEADER par:@{@"pageSize" : @"4", @"sort" : @"2"} success:^(id response) {

        _hotRadioArr = [LJCMusicModel modelHandleWithModel:response[@"data"]];/**< kvc */
        
        [_rootCollection reloadData];
    } filed:^(NSError *err) {
        
    }];
    
    /** 最新发声 */
    [LJCAFN POSTWithURL:@"http://api.pianke.me/version4.0/ting/list.php?sig=22A5BE98C01AFBE3142E550879F94685" prefix:KEYHEADER par:@{@"pageSize" : @"3", @"sort" : @"1"} success:^(id response) {
        
        _voicedArr = [LJCMusicModel modelHandleWithModel:response[@"data"]];
        
        
        
        [_rootCollection reloadData];
    } filed:^(NSError *err) {
        
    }];
}


/** 创建Collection */
- (void)createCollection {
    
    /** 创建layout */
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    /** 创建Collection */
    _rootCollection = [[UICollectionView alloc] initWithFrame:kScreen collectionViewLayout:layout];
    _rootCollection.delegate = self;
    _rootCollection.dataSource = self;
    [self.view addSubview:_rootCollection];
    _rootCollection.backgroundColor = [UIColor whiteColor];
    /** 注册 */
    [_rootCollection registerClass:[LJCMusicCollectionViewCell class] forCellWithReuseIdentifier:@"LJCMusicCollectionViewCell"];
    
    [_rootCollection registerClass:[LJCRadioCollectionViewCell class] forCellWithReuseIdentifier:@"LJCRadioCollectionViewCell"];
    
    [_rootCollection registerClass:[LJCMusicCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LJCMusicCollectionReusableView"];
}

#pragma mark - collection代理方法
/** 分区 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 5;
}

/** item数量 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return _titleArr.count;
    } else if (section == 1) {
        return _recommendArr.count;
    } else if (section == 2) {
        return _topTingArr.count;
    } else if (section == 3) {
        return _hotRadioArr.count;
    } else {
        return _voicedArr.count;
    }
}

/** itemSize */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    /** 按分区布局 */
    if (indexPath.section == 0) {

        return CGSizeMake((kScreenWidth - 5 * kSpacing) / 6, (kScreenWidth - 5 * kSpacing) / 6);
    } else if (indexPath.section == 1) {
        return CGSizeMake((kScreenWidth - 2 * kSpacing) / 3, (kScreenWidth - 4 * kSpacing) / 3 + 70);
    } else if (indexPath.section == 2) {
        return CGSizeMake((kScreenWidth - 2 * kSpacing) / 3, (kScreenWidth - 4 * kSpacing) / 3 + 70);
    } else if (indexPath.section == 3) {
        return CGSizeMake((kScreenWidth - kSpacing) / 2, (kScreenWidth - 3 * kSpacing) / 2 + 70);
    } else {
        return CGSizeMake((kScreenWidth - 2 * kSpacing) / 3, (kScreenWidth - 4 * kSpacing) / 3 + 70);
    }

}

/** cell赋值 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.section == 0) {
        LJCRadioCollectionViewCell *radioCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LJCRadioCollectionViewCell" forIndexPath:indexPath];
        [radioCell giveDataWithTitle:_titleArr[indexPath.row] Image:_imageArr[indexPath.row]];
        return radioCell;
    
    
    } else{
    LJCMusicCollectionViewCell *musicCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LJCMusicCollectionViewCell" forIndexPath:indexPath];
    
        if (indexPath.section == 1) {
            musicCell.musicModel = _recommendArr[indexPath.row];
            NSLog(@"%@", musicCell.musicModel.title);
        } else if (indexPath.section == 2) {
            musicCell.musicModel = _topTingArr[indexPath.row];
        } else if (indexPath.section == 3) {
            musicCell.musicModel = _hotRadioArr[indexPath.row];
        } else {
            musicCell.musicModel = _voicedArr[indexPath.row];
        }

        return musicCell;
    }

}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        LJCMusicCollectionReusableView *musicHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"LJCMusicCollectionReusableView" forIndexPath:indexPath];
        musicHeader.backgroundColor = [UIColor redColor];
        
        return musicHeader;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(kScreenWidth, 200);
    } else {
        return CGSizeMake(kScreenWidth, 50);
    }
    return CGSizeZero;
}

@end
